package com.tms.ned.controlmonitor.adapters;

import java.awt.Component;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

import com.tms.ned.controlmonitor.ComponentChangeEvent;
import com.tms.ned.controlmonitor.validate.Validator;

/*
 * Licensed under the Apache License, Version 2.0;<br> 
 * 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless 
 * required by applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and limitations under the License. 
 */


/**
 * Adapts a TextComponent for use with ControlMonitor. Handles document events,
 * and re-fires them as ControlChangeEvents.
 *
 * @author bshannon
 *
 */
public class TextComponentAdapter extends AbstractComponentAdapter  {

	/** The text component being adapted. (JTextField, etc.) * */
	private JTextComponent textComponent;

	/** Holds the initial value of the text field when it was registered. * */
	private String initialValue = null;

	private DocumentListener internalDocumentListener = new DocumentListener() {

		/** Implementation of DocumentListener * */
		public final void changedUpdate(DocumentEvent e) {
			handleChange();
		}

		public final void insertUpdate(DocumentEvent e) {
			handleChange();
		}

		public final void removeUpdate(DocumentEvent e) {
			handleChange();
		}

		/**
		 * Called by all document events, creates a ComponentChangeEvent and relies
		 * on parent to fireComponentChangeEvent().
		 */

	};


	/**
	 * Creates a TextComponentAdapter, for making TextComponents suitable for
	 * use with a ControlMonitor.
	 *
	 * @param name
	 *            The name of the textfield (for debugging or information)
	 * @param component
	 *            The actual JTextComponent
	 * @param validator
	 *            The (optional) Validator for the textfield.
	 */
	public TextComponentAdapter(String name, Component component, Validator validator) {
		super(name, component, validator);
		textComponent = (JTextComponent) component;
		//this.initialValue = textComponent.getText();
		textComponent.getDocument().addDocumentListener(internalDocumentListener);
		setInitialValue(textComponent.getText());
	}

	private final void handleChange() {
		ComponentChangeEvent event = new ComponentChangeEvent(TextComponentAdapter.this, TextComponentAdapter.this);
		fireComponentChangeEvent(event);
	}

	/**
	 * Returns the current value of the text component component.
	 */
	public final Object getCurrentValue() {
		return textComponent.getText();
	}


	/**
	 * Returns the initial value of the text component, which is the value that
	 * it had when it was adapted by this adapter.
	 */
	public final Object getInitialValue() {
		return initialValue;
	}

	/**
	 * You can use this method to override the initial value in order to affect
	 * the way this adapter considers the field "dirty".
	 */
	protected final void changeInitialValue(Object o) {
		initialValue = o + "";
		handleChange();
	}

}
